package com.hundredapp.oa.web.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.hundredapp.oa.domain.OaReserve;
import com.hundredapp.oa.domain.OaReturnVisit;
import com.hundredapp.oa.domain.OaSecuit;
import com.hundredapp.oa.repository.ReserveManager;
import com.hundredapp.oa.repository.ReturnVisitManager;
import com.hundredapp.oa.repository.SecuitManager;
import com.hundredapp.oa.repository.UserManager;
import com.hundredapp.oa.util.WebUtil;
import com.hundredapp.oa.vo.ReturnVisitVO;
import com.opensymphony.xwork2.ActionSupport;

@Repository
@Scope("prototype")
public class ReturnVisit extends ActionSupport {

	private List<ReturnVisitVO> root;
	
	private Object success;
	private String msg;
	private int id;
	private int start;
	private int limit;
	private int total;
	private int reserveid;
	private ReturnVisitVO returnvisitvo;
	private OaReturnVisit returnvisit;
	private OaSecuit secuit;
	
	@Autowired
	private ReserveManager reserveManager;
	@Autowired
	private ReturnVisitManager returnvisitManager;
	@Autowired
	private SecuitManager secuitManager;
	
	public String add() {
		try{
			returnvisit.setCreateUserId(WebUtil.GetLoginUserId());
			returnvisit.setOaReserve(reserveManager.findReserveById(id));
			returnvisitManager.add(returnvisit);
			success=true;
			msg="确认成功";
		}catch(Exception e) {
			e.printStackTrace();
			success=false;
			msg="确认失败";
		}
		return SUCCESS;
	}
	
	public String find() {
		returnvisit=returnvisitManager.findReturnVisitById(id);
		returnvisitvo=new ReturnVisitVO();
		returnvisitvo.setId(returnvisit.getId());
		returnvisitvo.setReserveTime(returnvisit.getOaReserve().getReserveTime());
		returnvisitvo.setRVisitContent(returnvisit.getRVisitContent());
		returnvisitvo.setRVisitTime(returnvisit.getRVisitTime());
		returnvisitvo.setSecuitName(returnvisit.getOaReserve().getOaSecuit().getDisplayName());
		returnvisitvo.setUserName(returnvisit.getOaReserve().getOaUser().getDisplayName());
		return SUCCESS;
	}
	
	public String getbysecuit() {
		OaSecuit sect=secuitManager.findSecuitById(id);
		root=new ArrayList<ReturnVisitVO>();
		for(OaReserve or:sect.getOaReserves()) {
			for(OaReturnVisit orv:or.getOaReturnVisits()) {
				ReturnVisitVO temp=new ReturnVisitVO();
				temp.setId(orv.getId());
				temp.setReserveTime(or.getReserveTime());
				temp.setRVisitContent(orv.getRVisitContent());
				temp.setRVisitTime(orv.getRVisitTime());
				temp.setSecuitName(sect.getDisplayName());
				temp.setUserName(or.getOaUser().getDisplayName());
				root.add(temp);
			}
		}
		return SUCCESS;
	}
	
	public List<ReturnVisitVO> getRoot() {
		return root;
	}
	public void setRoot(List<ReturnVisitVO> root) {
		this.root = root;
	}
	public Object getSuccess() {
		return success;
	}
	public String getMsg() {
		return msg;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public int getReserveid() {
		return reserveid;
	}
	public void setReserveid(int reserveid) {
		this.reserveid = reserveid;
	}
	public OaReturnVisit getReturnvisit() {
		return returnvisit;
	}
	public void setReturnvisit(OaReturnVisit returnvisit) {
		this.returnvisit = returnvisit;
	}

	public ReturnVisitVO getReturnvisitvo() {
		return returnvisitvo;
	}

	public void setReturnvisitvo(ReturnVisitVO returnvisitvo) {
		this.returnvisitvo = returnvisitvo;
	}

	public OaSecuit getSecuit() {
		return secuit;
	}

	public void setSecuit(OaSecuit secuit) {
		this.secuit = secuit;
	}
}
